package com.smart.lab.view.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.smart.lab.R;
import com.smart.lab.databinding.ActivityClipBinding;
import com.smart.lab.databinding.ActivityScanResultBinding;
import com.smart.lab.model.CropPestDiseaseEntity;
import com.smart.lab.utils.GlideImageLoader;
import com.smart.lab.view.adapter.DetailPageAdapter;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

public class ScanResultActivity  extends AppCompatActivity {

    ActivityScanResultBinding binding;

    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private FragmentManager fm;

    private Banner banner;

    private CropPestDiseaseEntity cropPestDiseaseEntity;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityScanResultBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());


        // Set the Toolbar as the ActionBar
        setSupportActionBar(binding.toolbar);

        // Enable the back button (home button) and set its visibility
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.mipmap.back);


        cropPestDiseaseEntity=(CropPestDiseaseEntity)getIntent().getSerializableExtra("entity");

        binding.toolbar.setTitle(cropPestDiseaseEntity.getName()+"详情");
        banner=binding.slider;


        mTabLayout= binding.slidingTabs;
        mViewPager= binding.viewpager;
        fm = getSupportFragmentManager();
//        为viewpager设置适配器
        mViewPager.setAdapter(new DetailPageAdapter(ScanResultActivity.this, fm,cropPestDiseaseEntity));
        mTabLayout.setupWithViewPager(mViewPager);

        initSlider();


    }


    private void initSlider() {

//        final List<String> imgList = new ArrayList<>();
//        for (int i = 0; i < _CarouselAdvList.size(); i++) {
//            imgList.add(_CarouselAdvList.get(i).ImageUrl);
//        }
//        List<String> titleList = new ArrayList<String>();
//        for (int i = 0; i < _CarouselAdvList.size(); i++) {
//            titleList.add(_CarouselAdvList.get(i).AdvertisementTile);
//        }

        List<String> imgList = new ArrayList<>();
//        imgList.add("http://120.224.86.116:28284/group1/M00/00/0E/wKgKtV-AAmSAQkwBABJqrHX6jK0815.jpg");
//        imgList.add("http://120.224.86.116:28284/group1/M00/00/0E/wKgKtV-AAnGASu-VAB34b_rnFrA173.jpg");
//        imgList.add("http://120.224.86.116:28284/group1/M00/00/0E/wKgKtV-NALmAKXSIAAEbSOePeAA797.jpg");
        imgList.add("https://bkimg.cdn.bcebos.com/pic/8718367adab44aed8718d646ba1c8701a18bfb3a");
//        imgList.add("http://120.224.86.116:28284/group1/M00/00/0E/wKgKtV-AAnGASu-VAB34b_rnFrA173.jpg");
//        imgList.add("http://120.224.86.116:28284/group1/M00/00/0E/wKgKtV-NALmAKXSIAAEbSOePeAA797.jpg");

        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(imgList);

        //设置banner动画效果
        banner.setBannerAnimation(Transformer.DepthPage);
        //设置标题集合（当banner样式有显示title时）
        // banner.setBannerTitles(titleList);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(1500 * imgList.size());
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CENTER);
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                //点击事件
            }
        });

//        banner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//            }
//
//            @Override
//            public void onPageSelected(int position) {
////                Log.d(TAG, "onPageSelected:" + position);
//
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//
//            }
//        });

        //banner设置方法全部调用完毕时最后调用
        banner.start();

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
